package Search2017;

/**
 * 二分查找
 * Created by 雷文康 on 2017/12/27.
 */
public class BinarySearch {
    public static void main(String[] args) {
        int[] array={1,2,4,5,25,63,72,81};
        System.out.println(binarySearch(array,25));
    }
    public static int binarySearch(int[] array, int num){
        if (array == null || array.length == 0) {
            return -1;
        }
        int low = 0;
        int high = array.length - 1;
        int mid;
        while(low <= high) {
            mid = (low + high) / 2;
            if (num == array[mid]){
                return mid;
            } else if (num < array[mid]) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;

    }
}
